package com.jingdianjichi.subject.controller;

import com.jingdianjichi.subject.common.entity.PageResult;
import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.controller.convert.SubjectAnswerDTOConverter;
import com.jingdianjichi.subject.controller.convert.SubjectInfoDTOConverter;
import com.jingdianjichi.subject.controller.dto.SubjectInfoDTO;
import com.jingdianjichi.subject.domain.entity.SubjectAnswerBO;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBO;
import com.jingdianjichi.subject.domain.service.SubjectInfoDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 刷题controller
 */
@RestController
@RequestMapping("/subject")
@Slf4j
public class SubjectController {
    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;



    /**
     * 新增题目
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO) {


            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);
            List<SubjectAnswerBO> subjectAnswerBOS = SubjectAnswerDTOConverter.INSTANCE
                    .convertListDTOToBO(subjectInfoDTO.getOptionList());
            subjectInfoBO.setOptionList(subjectAnswerBOS);
            subjectInfoDomainService.add(subjectInfoBO);

        log.info("添加成功");
        return Result.success(true);


    }

    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
//            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(), "分类Id不能为空");
//            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(), "标签Id不能为空");
            SubjectInfoBO subjectInfoBo = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);
            PageResult<SubjectInfoBO> subjectInfoBoPageResult = subjectInfoDomainService.getSubjectPage(subjectInfoBo);
            return Result.success(subjectInfoBoPageResult);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("/querySubjectInfo")
    public Result<?> querySubjectInfo (@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
//            Preconditions.checkNotNull(subjectInfoDTO.getId(), "题目Id不能为空");
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);
            SubjectInfoBO querySubjectInfo  =subjectInfoDomainService.querySubjectInfo(subjectInfoBO);
            SubjectInfoDTO infoDTO = SubjectInfoDTOConverter.INSTANCE.converSingleDto(querySubjectInfo);
            return Result.success(infoDTO);
        }catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }



}
